//numstr.cpp -- following number input with line input
//string.cpp -- storing string in an array
//arrayone.cpp -- small arrays of integers
//strtype.cpp -- line input
//use_new -- using the new operator
//arraynew.cpp -- using the new opreator for arrays
//ptrstr.cpp -- using pointers to strings
//newstruct -- using new with a strcucture

#include <iostream>
#include <cstring>
#include <string>
#include <vector> //STL C++98
#include <array> //C++11
void Example4_1();
void Example4_2();
void Example4_6();
void Example4_10();
void Example4_3_5();
void Example4_13();
void Example4_17();
void Example4_18();
void Example4_20();
void Example4_21();
void Example4_23();
void Example4_24();

//int main()
//{
//	Example4_24();
//	return 0;
//}

void Example4_1()
{
	//just test sizeof
	using namespace std;
	int yams[3]{ 7,8,6 };
	cout << "Size of yams array = " << sizeof yams << " bytes.\n";
	cout << "Size of one element = " << sizeof (yams[0]) << " bytes." << endl;
	return;
}

void Example4_2()
{
	using namespace std;
	const int Size = 15;
	char name1[Size]; //empty array
	char name2[Size] = "C++owboy"; //initialized array
	cout << "Howdy! I'm " << name2;
	cout << "! What's your name?\n";
	cin >> name1;
	cout << "Well, " << name1 << ", your name has ";
	cout << strlen(name1) << " letters and is stored\n";
	return;
}

void Example4_6()
{
	using namespace std;
	cout << "What year was your house built?\n";
	int year;
	cin >> year;
	cin.get();
	cout << "What is its street address?\n";
	char address[80];
	cin.get(address, 80);
	cout << "Year built: " << year << endl;
	cout << "Address: " << address << endl;
	cout << "Done!\n";
	return;
}

void Example4_10()
{
	using namespace std;
	string s1 = "ya";
	string s2, s3, s4;
	cout << "please input a word:" << endl;
	(cin >> s2).get(); // can't input " "
	cout << "please input a sentence:" << endl;
	getline(cin, s3);  // 
	s4 = s1 + s2 +s3;
	cout << s4;
	return;
}

void Example4_3_5()
{
	using namespace std;
	wchar_t title[]{ L"Chief Astrogator" };
	char16_t name[]{ u"Felonia Ripova" };
	char32_t car[]{ U"Humber Super Snipe" };
	string name2{ u8"yoyoyo" };
	cout << "UTF-8: " << name2 << endl;
	cout << "char32_t: " << car << endl;

	//C++11 new string raw
	cout << "C++11 new string raw:" << endl;
	cout << R"(Jim "King" Tutt uses "\n" instead of endl.)" << '\n';
	cout << R"+*("(Who wouldn't?)", she whispered.)+*" << endl;
	return;
}

void Example4_13()
{
	using namespace std;

	struct inflatable
	{
		char name[20];
		float volume;
		double price;
	};

	inflatable guests[2]=
	{
		{"Bambi", 0.5 , 21.99},
		{"Godzilla", 2000, 565.99}
	};

	cout << "The guests " << guests[0].name << endl;

	//union
	union one4all
	{
		int int_val;
		double double_val;
	};

	one4all pail;
	pail.int_val = 15; //store an int
	cout << pail.int_val << endl;
	pail.double_val = 1.38; //store a double, int value is lost
	cout << pail.double_val << endl;

	//enum 
	enum bits{one = 1 , two = 2 ,four = 4 , eight = 8};
	bits myflag{ bits(6) };
	cout << myflag << endl;
	return;
}

void Example4_17()
{
	using namespace std;
	int nights = 1001;
	int * pt = new int; // allocate space for int
	*pt = 1001; //store a value there

	double * pd = new double;
	*pd = 10000001.0;
	cout << "nights value = ";
	cout << nights << ": location " << &nights << endl;

	cout << "size of *pt = " << sizeof(*pt) << endl;
	cout << "size of *pd = " << sizeof(*pd) << endl;
	cout << "size of pt = " << sizeof(pt) << endl;
	cout << "size of pd = " << sizeof(pd) << endl;
	return;
}

void Example4_18()
{
	using namespace std;
	double * p3 = new double[3];//space for 3 doubles
	p3[0] = 0.2;                //treat p3 like an array name
	p3[1] = 0.5;
	p3[2] = 0.8;
	cout << "p3[1] is " << p3[1] << ".\n";
	p3 = p3 + 1;                //increment the pointer
	cout << "Now p3[0] is " << p3[0] << " and ";
	cout << "p3[1] is " << p3[1] << ".\n";
	p3 = p3 - 1;//point back to beginning
	delete [] p3; //free the memory
	return;
}

void Example4_20()
{
	using namespace std;
	char animal[20] = "bear"; //animal holds bear
	const char * bird = "wren";//bird holds address of string
	char * ps;                //uninitialized

	cout << animal << " and ";//display bear
	cout << bird << "\n";     //display wren
	//cout << ps << "\n"  //may display garbage, may cause a crash

	cout << "Enter a kind of animal: ";
	cin >> animal;         //ok if input < 20 chars
	//cin >> ps; Too horrible a blunder to try; ps doesn't point to allocated space

	ps = animal; //set ps to point to string
	cout << ps << "!\n"; //ok, same as using animal
	cout << "Before using strcpy():\n";
	cout << animal << " at " << (int *)animal << endl;
	cout << ps << " at " << (int *)animal << endl;

	ps = new char[strlen(animal) + 1];//get new storage
	strcpy(ps, animal);//copy string to new storage
	cout << "After using strcpy():\n";
	cout << animal << " at " << (int *)animal << endl;
	cout << ps << " at " << (int *)ps << endl;
	delete[] ps;
	return;
}

void Example4_21()
{
	struct inflatable // strcture definition
	{
		char name[20];
		float volume;
		double price;
	};

	using namespace std;
	inflatable *ps = new inflatable; //allot memory for structure
	cout << "Enter name of inflatable item: ";
	cin.get(ps->name, 20);          //method 1 for member access
	cout << "Enter volume in Cubic feet: ";
	cin >> (*ps).volume;            //method 2 for member access
	cout << "Enter price : $";
	cin >> ps->price;
	cout << "Name: " << (*ps).name << endl;
	cout << "Volume: " << ps->volume << "cubic feet\n";
	cout << "Price: $" << ps->price << endl;
	delete ps;
	return;
}

void Example4_23()
{
	struct antarctica_years_end
	{
		int year;
		/*some really interesting data, etc.*/
	};

	antarctica_years_end s01, s02, s03;
	s01.year = 1998;
	antarctica_years_end * pa = &s02;
	pa->year = 1999;
	antarctica_years_end trio[3];//array of 3 structures
	trio[0].year = 2003;
	std::cout << trio->year << std::endl;
	const antarctica_years_end * arp[3] = { &s01,&s02,&s03};
	std::cout << arp[1]->year << std::endl;
	const antarctica_years_end ** ppa = arp;
	auto ppb = arp;       //C++11 automatic type deduction
	//or else use const antarctica_years_end ** ppb = arp;
	std::cout << (*ppa)->year << std::endl;
	std::cout << (*(ppb + 1))->year << std::endl;
	return;
}

void Example4_24()
{
	using namespace std;
	//C, original C++
	double a1[4]  { 1.2,2.4,3.6,4.8 };
	//C++98 STL
	vector<double> a2(4); //create vector with 4 elements
	//no simple way to initialize in C98
	a2[0] = 1.0 / 3.0;
	a2[1] = 1.0 / 4.0;
	a2[2] = 1.0 / 5.0;
	a2[3] = 1.0 / 6.0;
	//C++11 -- create and initialize array object
	array<double, 4>a3 = { 3.14,2.72,1.62,1.41 };
	array<double, 4>a4;
	a4 = a3;//valid for array objects of same size
	//use array notation
	cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
	cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
	cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
	cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
	//misdeed 
	a1[-200] = 20.2;
	cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
	cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
	cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
	return;
}
